home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / PolygonAnimation.java < prev    next >
Text File  |  1998-10-28  |  2KB  |  93 lines

  1. /*
  2.     This class is a simple extention of Canvas that runs as it's own
  3.     thread.  It runs a simple animation.
  4.  */
  5.  
  6. import java.awt.*;
  7.  
  8. class PolygonAnimation extends Canvas implements Runnable
  9. {
  10.     private Thread  myThread;
  11.     private Polygon thePolygon;
  12.     private int     polyX[] = new int[9],  //The polygons X coordinates
  13.                     polyY[] = new int[9],  //The polygons Y coordinates
  14.                     position,              //The point on the polygon the line is drawn to
  15.                     delay = 100;
  16.  
  17.     public PolygonAnimation()
  18.     {
  19.         setSize(130, 130);
  20.         setupPolygon();
  21.         thePolygon = new Polygon(polyX, polyY, 9);
  22.         position = 0;
  23.  
  24.         myThread = new Thread(this);
  25.         myThread.start();
  26.     }
  27.  
  28.     public PolygonAnimation(int _delay)
  29.     {
  30.         this();
  31.         if(_delay < 50) delay = 50;
  32.         else if(_delay > 300) delay = 300;
  33.         else delay = _delay;
  34.     }
  35.  
  36.     public void reset() {position = 0;}
  37.  
  38.     public void resume() { myThread.resume(); }
  39.  
  40.     public void run()
  41.     {
  42.         while (true)
  43.         {
  44.             repaint();
  45.             try{ Thread.sleep(delay); } catch(InterruptedException e) { this.stop(); }
  46.             advance();
  47.         }
  48.     }
  49.  
  50.     public void stop() { myThread.suspend(); }
  51.  
  52.     public void destroy() { this.stop(); }
  53.  
  54.     //Draw the polygon and a line from the center to the
  55.     //specified position.
  56.     public void paint(Graphics g)
  57.     {
  58.         g.drawPolygon(thePolygon);
  59.         g.drawLine(65, 65, polyX[position], polyY[position]);
  60.     }
  61.  
  62.     private void advance()
  63.     {
  64.         if(position < 6) position++;
  65.         else position = 0;
  66.     }
  67.  
  68.     //Set up the two arrays with the X and Y coordinate values
  69.     private void setupPolygon()
  70.     {
  71.         polyX[0] = 37;
  72.         polyX[1] = 97;
  73.         polyX[2] = 129;
  74.         polyX[3] = 129;
  75.         polyX[4] = 97;
  76.         polyX[5] = 37;
  77.         polyX[6] = 1;
  78.         polyX[7] = 1;
  79.         polyX[8] = 37;
  80.  
  81.         polyY[0] = 1;
  82.         polyY[1] = 1;
  83.         polyY[2] = 37;
  84.         polyY[3] = 97;
  85.         polyY[4] = 129;
  86.         polyY[5] = 129;
  87.         polyY[6] = 97;
  88.         polyY[7] = 37;
  89.         polyY[8] = 1;
  90.     }
  91. }
  92.  
  93.